package com.zcpt.controller.realname;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zcpt.pojo.users.Realname;
import com.zcpt.realname.IRealNameService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@Api(tags = "实名认证接口")
@Controller
@RequestMapping("/realname")
public class RealnameController {
    @Reference
    private IRealNameService realNameService;

    //添加实名信息 fileurl待写
    @PostMapping("/add")
    @ApiOperation(value = "添加实名信息", notes = "获取待审核实名信息")
    public String addRealname(@RequestParam("img_file") MultipartFile img_file,Realname realname) throws IOException {
        if (realname != null) {
            System.out.println("realname存在");

            //将realname传给service
            System.out.println(img_file);
            if (img_file != null){
                String filename = img_file.getOriginalFilename();
                String fileurl = "/opt/img/" + filename;
                realname.setImg(fileurl);
                img_file.transferTo(new File(fileurl));
                realNameService.addRealName(realname);
                return "index";
            }
        }
        else
            System.out.println("失败");
            return "forward:/fail.html";
    }

//    //高效字节流一次读取一个字节数组
//    private static void transfor(File src, String dest) throws IOException {
//        //封装数据源
//        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
//        //封装目的地
//        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
//
//        byte[] bys = new byte[1024];
//        int len = 0;
//        while ((len = bis.read(bys)) != -1) {
//            bos.write(bys, 0, len);
//        }
//        bos.close();
//        bis.close();
//    }


}
